package com.lf.config;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import com.lf.weixinOriginPay.sdk.WeChatTask;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 启动监听器
 *
 * @author Storezhang
 */
//@Service
//@Order(value=500)
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    public void onApplicationEvent(ContextRefreshedEvent evt) {
        if (evt.getApplicationContext().getParent() == null) {
            new Thread(new Runnable() {
                public void run() {
                    logger.info(">>>>>>>>>>>>>>>服务启动后执行，执行加载数据等操作 11111111 <<<<<<<<<<<<<");
                    while (true){
                        try {
                            logger.info("js  access_token 开始刷新！");
//                        JSONObject jsonObject =  JSONObject.parseObject(HttpUtils.sendGet("http://www.wwhtjy.com/wei/xin/public/get/access/token",new HashMap<String,String>()));
//                        logger.info("jsonObject : "+jsonObject);
//                        JSONObject jsondata = jsonObject.getJSONObject("data");
//                        logger.info("jsondata : "+jsondata);
//                        String access_tokens = jsondata.getString("access_token");
//                        logger.info("access_tokens : "+access_tokens);
//                        JSONObject jsapi_ticket = JSONObject.parseObject(HttpUtils.sendGet("http://www.wwhtjy.com/wei/xin/public/get/jsapi/ticket",new HashMap<String,String>()));
//                        logger.info("jsapi_ticket : "+jsapi_ticket.getJSONObject("data").getString("ticket"));
//
//                        WeChatTask.JSAPI_TICKET =  jsapi_ticket.getJSONObject("data").getString("ticket");
//                        WeChatTask.ACCESS_TOKEN = access_tokens;
//                        WeChatTask.getToken_getTicket(redisService.getString("WX_APPID"),redisService.getString("WX_APPID"));
//                        logger.info("WeChatTask.JSAPI_TICKET===="+WeChatTask.JSAPI_TICKET);
//                        logger.info("WeChatTask.ACCESS_TOKEN===="+WeChatTask.ACCESS_TOKEN);
//                        Thread.sleep(1000 * 7000);

//                          if ("".equals(WeChatTask.JSAPI_TICKET)||"".equals(WeChatTask.ACCESS_TOKEN) ){
//                            logger.info("js  access_token 开始刷新！");
//                            WeChatTask.getToken_getTicket();
//                            logger.info("WeChatTask.JSAPI_TICKET===="+WeChatTask.JSAPI_TICKET);
//                            logger.info("WeChatTask.ACCESS_TOKEN===="+WeChatTask.ACCESS_TOKEN);
//                          }else{
//                            Thread.sleep(1000 * 3600);
//                            WeChatTask.JSAPI_TICKET = "";
//                            WeChatTask.ACCESS_TOKEN = "";
//                            logger.info("js  access_token 开始刷新！");
//                            logger.info("WeChatTask.JSAPI_TICKET===="+WeChatTask.JSAPI_TICKET);
//                            logger.info("WeChatTask.ACCESS_TOKEN===="+WeChatTask.ACCESS_TOKEN);
//                          }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }


}